<?php
namespace logic\set\cases;
/**
 * 案例栏目管理
 */
class CaseGroupManage extends \logic\Base{
    
    
    /**
     * 读取全部列表
     */
    public function getList($param){
        return D('CaseGroup','setCases')->getSimpleList($param);
    }
    
    /**
     * 读取树结构列表
     */
    public function getTreeList($param){
        $list = D('CaseGroup','setCases')->getSimpleList($param);
        $list1 = $list2 = [];
        foreach( $list as $key => $val ){
            if( $val['status'] == 1 ) $list1[] = $val;
            if( $val['status'] == 2 ) $list2[] = $val;
        }
        if( $list1 ) $list1 = \core\helper\Tools::arrayMerge($list1);
        if( $list2 ) $list2 = \core\helper\Tools::arrayMerge($list2);
        return array_merge($list1,$list2);        
    }
    /**
     * 添加
     */
    public function add($param){
        $param['siteid'] =  $this->getSiteId();
        if( $param['pid'] ){
            $pinfo = D('CaseGroup','setCases')->find(['id'=>$param['pid'],'siteid'=>$param['siteid']]);
            if( !$pinfo ) logicE('所属类目不存在');
            $param['pid']   = $pinfo['id'];
            $param['depth'] = $pinfo['depth'] + 1;
        }        
        $id = D('CaseGroup','setCases')->add($param);
        return $id;
    }
    
    /**
     * 修改全部
     */
    public function editAll($data,$siteId){
        $save = $this->getEditData($data);
       // $siteId = $this->getSiteId();
        try{
            $model = M();
            $model->startTrans();
            foreach( $save as $key => $val ){
                D('CaseGroup','setCases')->edit(['id'=>$val['id'],'siteid'=>$siteId],$val);
            }
            $model->commit();
            return true; 
        }catch( \think\Exception $e){
            $model->rollBack();
            logicE($e->getMessage());
        }
    }
    
    /**
     * 组装数据
     */
    private function getEditData($data,$pid = 0,$depth = 1){
        $num = count($data)+10;
        $returnList = [];
        foreach( $data as $key=> $val ){
            $returnList[] = [
                'id'    => $val['id'],
                'pid'   => $pid,
                'groupname' => $val['groupname'],
                'depth'     => $depth,
                'sort'      => $num - $key
            ];
            if( is_array( $val['child']) ){
                $childList = $this->getEditData($val['child'],$val['id'],$depth+1);
                $returnList = array_merge($returnList,$childList);
            }
        }
        return $returnList;
        
    }
    /**
     * 修改状态
     */
    public function editStatus($id,$siteId){
        $info = D('CaseGroup','setCases')->find(['id'=>$id,'siteid'=>$siteId]);
        if( !$info ) logicE('数据不存在');
        $status = $info['status'] == 1 ? 2 : 1;
        try{
            $model = M();
            $model->startTrans();
            $save = [
                'status'    =>$status,
                'depth'     =>1,
                'pid'       =>0,
                ];
            D('CaseGroup','setCases')->edit(['id'=>$id,'siteid'=>$siteId],$save);
            $this->editChildStatus($id,$siteId,$status,2);
            $model->commit();
        }catch( \think\Exception $e){
            $model->rollback();
            logicE($e->getMessage());
        }
        return true;
    }
    
    
    /**
     * 根据父级ID 关闭或开启 所有子集
     * @param $pid 父级ID 操作依据
     * @param $status 更改的目标状态
     * @param $depth 更改的目标状态 深度
     */
    private function editChildStatus($pid,$siteId,$status,$depth = 1){
        $list = D('CaseGroup','setCases')->getSimpleList(['pid'=>$pid,'siteid'=>$siteId]);
        if( $list ){
            $ids = array_key_column($list,'id');
            D('CaseGroup','setCases')->edit(['id'=>$ids,'siteid'=>$siteId],['status'=>$status,'depth'=>$depth]);            
            return $this->editChildStatus($ids,$siteId,$status,$depth+1);
        }
        return true;
    }
}